@@ -1,3 +1,4 @@ |
||
| 1 | 1 |
.idea/ |
| 2 | 2 |
.DS_Store |
| 3 |
-minipai2.db |
|
| 3 |
+minipai2.db |
|
| 4 |
+minipai2.log |
@@ -0,0 +1,9 @@ |
||
| 1 |
+## Start |
|
| 2 |
+* Default Port |
|
| 3 |
+``` |
|
| 4 |
+nohup python main.py &>minipai2.log & |
|
| 5 |
+``` |
|
| 6 |
+* Define Port |
|
| 7 |
+``` |
|
| 8 |
+nohup python main.py --port=8002 &>minipai2.log & |
|
| 9 |
+``` |
@@ -1,5 +1,6 @@ |
||
| 1 | 1 |
# -*- coding: utf-8 -*- |
| 2 | 2 |
|
| 3 |
+import errno |
|
| 3 | 4 |
import glob |
| 4 | 5 |
import os |
| 5 | 6 |
import sqlite3 |
@@ -17,7 +18,7 @@ options.parse_command_line() |
||
| 17 | 18 |
|
| 18 | 19 |
ROOT_PATH = '/tmp/minipai2' |
| 19 | 20 |
|
| 20 |
-# 创建数据表语句 |
|
| 21 |
+# Create Table SQL |
|
| 21 | 22 |
CREATE_TABLE_STMT = """ |
| 22 | 23 |
CREATE TABLE IF NOT EXISTS photoinfo ( |
| 23 | 24 |
id integer primary key, |
@@ -25,13 +26,15 @@ CREATE_TABLE_STMT = """ |
||
| 25 | 26 |
session varchar(20), |
| 26 | 27 |
name varchar(13) |
| 27 | 28 |
);""" |
| 28 |
-# 创建索引语句 |
|
| 29 |
+# Create Index SQL |
|
| 29 | 30 |
CREATE_INDEX1 = 'CREATE INDEX IF NOT EXISTS idx_lensman ON photoinfo (lensman);' |
| 30 | 31 |
CREATE_INDEX2 = 'CREATE INDEX IF NOT EXISTS idx_session ON photoinfo (session);' |
| 31 |
-# 插入数据语句 |
|
| 32 |
-INSERT_RECORD_STMT = 'INSERT INTO photoinfo VALUES (NULL, ?, ?, ?)' |
|
| 33 |
-# 查询数据语句 |
|
| 34 |
-SELECT_RECORD_STMT = 'SELECT MAX(name) FROM photoinfo WHERE lensman = ? and session = ?' |
|
| 32 |
+# Insert Record SQL |
|
| 33 |
+INSERT_RECORD_STMT = 'INSERT INTO photoinfo VALUES (NULL, ?, ?, ?);' |
|
| 34 |
+# Delete Record SQL |
|
| 35 |
+DELETE_RECORD_STMT = 'DELETE FROM photoinfo WHERE lensman = ? and session = ? and name = ?;' |
|
| 36 |
+# Query Max(name) SQL |
|
| 37 |
+SELECT_RECORD_STMT = 'SELECT MAX(name) FROM photoinfo WHERE lensman = ? and session = ?;' |
|
| 35 | 38 |
|
| 36 | 39 |
|
| 37 | 40 |
conn = sqlite3.connect('minipai2.db')
|
@@ -43,14 +46,35 @@ cur.execute(CREATE_INDEX2) |
||
| 43 | 46 |
conn.commit() |
| 44 | 47 |
|
| 45 | 48 |
|
| 49 |
+# FILE OPERATE |
|
| 50 |
+def silent_makdirs(path): |
|
| 51 |
+ try: |
|
| 52 |
+ os.makedirs(path) |
|
| 53 |
+ except OSError as e: |
|
| 54 |
+ if e.errno != errno.EEXIST: |
|
| 55 |
+ raise |
|
| 56 |
+ |
|
| 57 |
+ |
|
| 58 |
+def silent_remove(path): |
|
| 59 |
+ try: |
|
| 60 |
+ os.remove(path) |
|
| 61 |
+ except OSError as e: |
|
| 62 |
+ if e.errno != errno.ENOENT: |
|
| 63 |
+ raise |
|
| 64 |
+ |
|
| 65 |
+ |
|
| 66 |
+def get_session_root(lensman, session): |
|
| 67 |
+ return '{}/{}'.format(ROOT_PATH, session)
|
|
| 68 |
+ |
|
| 69 |
+ |
|
| 46 | 70 |
def get_session_dir(lensman, session): |
| 47 |
- return '{}/{}/{}'.format(ROOT_PATH, session, 'origin'), '{}/{}/{}'.format(ROOT_PATH, session, 'thumbnail')
|
|
| 71 |
+ session_root = get_session_root(lensman, session) |
|
| 72 |
+ return '{}/{}'.format(session_root, 'origin'), '{}/{}'.format(session_root, 'thumbnail')
|
|
| 48 | 73 |
|
| 49 | 74 |
|
| 50 | 75 |
def create_session_dir(lensman, session): |
| 51 | 76 |
for path in get_session_dir(lensman, session): |
| 52 |
- if not os.path.exists(path): |
|
| 53 |
- os.makedirs(path) |
|
| 77 |
+ silent_makdirs(path) |
|
| 54 | 78 |
|
| 55 | 79 |
|
| 56 | 80 |
def get_last_timestamp(lensman, session): |
@@ -64,6 +88,11 @@ def insert_session_file(lensman, session, name): |
||
| 64 | 88 |
conn.commit() |
| 65 | 89 |
|
| 66 | 90 |
|
| 91 |
+def delete_session_file(lensman, session, name): |
|
| 92 |
+ cur.execute(DELETE_RECORD_STMT, (lensman, session, name)) |
|
| 93 |
+ conn.commit() |
|
| 94 |
+ |
|
| 95 |
+ |
|
| 67 | 96 |
def get_new_files(lensman, session, maxt): |
| 68 | 97 |
_, thumb = get_session_dir(lensman, session) |
| 69 | 98 |
files = glob.iglob('{}/*'.format(thumb))
|
@@ -90,7 +119,10 @@ class SessionStartHandler(RequestHandler): |
||
| 90 | 119 |
def post(self): |
| 91 | 120 |
lensman = self.get_argument('lensman', '')
|
| 92 | 121 |
session = self.get_argument('session', '')
|
| 122 |
+ |
|
| 93 | 123 |
create_session_dir(lensman, session) |
| 124 |
+ os.chmod(get_session_root(lensman, session), 0777) |
|
| 125 |
+ |
|
| 94 | 126 |
self.write({
|
| 95 | 127 |
'status': 200, |
| 96 | 128 |
}) |
@@ -100,6 +132,9 @@ class SessionEndHandler(RequestHandler): |
||
| 100 | 132 |
def post(self): |
| 101 | 133 |
lensman = self.get_argument('lensman', '')
|
| 102 | 134 |
session = self.get_argument('session', '')
|
| 135 |
+ |
|
| 136 |
+ os.chmod(get_session_root(lensman, session), 0700) |
|
| 137 |
+ |
|
| 103 | 138 |
self.write({
|
| 104 | 139 |
'status': 200, |
| 105 | 140 |
}) |
@@ -138,12 +173,52 @@ class FetchOriginHandler(RequestHandler): |
||
| 138 | 173 |
}) |
| 139 | 174 |
|
| 140 | 175 |
|
| 176 |
+class DeletePhotoHandler(RequestHandler): |
|
| 177 |
+ def post(self): |
|
| 178 |
+ lensman = self.get_argument('lensman', '')
|
|
| 179 |
+ session = self.get_argument('session', '')
|
|
| 180 |
+ |
|
| 181 |
+ id_ = self.get_argument('id', '')
|
|
| 182 |
+ name = self.get_argument('name', '')
|
|
| 183 |
+ path = self.get_argument('path', '')
|
|
| 184 |
+ |
|
| 185 |
+ # Delete Record from Sqlite3 |
|
| 186 |
+ delete_session_file(lensman, session, id_) |
|
| 187 |
+ |
|
| 188 |
+ # Delete Photo from Disk |
|
| 189 |
+ origin, thumb = get_session_dir(lensman, session) |
|
| 190 |
+ # Delete Thumbnail |
|
| 191 |
+ silent_remove('{}/{}'.format(thumb, name))
|
|
| 192 |
+ # Delete Origin |
|
| 193 |
+ silent_remove('{}/{}'.format(origin, name))
|
|
| 194 |
+ |
|
| 195 |
+ self.write({
|
|
| 196 |
+ 'status': 200, |
|
| 197 |
+ }) |
|
| 198 |
+ |
|
| 199 |
+ |
|
| 200 |
+class PrintQRCodeHandler(RequestHandler): |
|
| 201 |
+ def post(self): |
|
| 202 |
+ lensman = self.get_argument('lensman', '')
|
|
| 203 |
+ session = self.get_argument('session', '')
|
|
| 204 |
+ |
|
| 205 |
+ # Call ``C`` to Connect Printer |
|
| 206 |
+ # Param |
|
| 207 |
+ # :: session |
|
| 208 |
+ |
|
| 209 |
+ self.write({
|
|
| 210 |
+ 'status': 200, |
|
| 211 |
+ }) |
|
| 212 |
+ |
|
| 213 |
+ |
|
| 141 | 214 |
handlers = [ |
| 142 | 215 |
(r'/', HelloHandler), |
| 143 | 216 |
(r'/session_start', SessionStartHandler), |
| 144 | 217 |
(r'/session_end', SessionEndHandler), |
| 145 | 218 |
(r'/fetch_thumbnail', FetchThumbnailHandler), |
| 146 | 219 |
(r'/fetch_origin', FetchOriginHandler), |
| 220 |
+ (r'/delete_photo', DeletePhotoHandler), |
|
| 221 |
+ (r'/print_qrcode', PrintQRCodeHandler), |
|
| 147 | 222 |
(r'/static/(.*)', StaticFileHandler, {'path': ROOT_PATH}),
|
| 148 | 223 |
] |
| 149 | 224 |
|